package com.foreknow.service.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 业务层需要处理事务
 */
public class AdminServiceImpl implements AdminService {
    @Override
    public boolean add(Admin admin) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用dao层的save方法
        AdminDaoImpl adminDao = new AdminDaoImpl();
        //注入连接
        adminDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = adminDao.save(admin);
            conn.commit();// 提交事务
        } catch (SQLException e) {
            try {
                conn.rollback(); //回滚
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean deleteById(int id) {
        return false;
    }

    @Override
    public Admin getQueryById(int id) {
        return null;
    }

    @Override
    public boolean update(Admin admin) {
        return false;
    }

    @Override
    public List<Object> getQueryAll() {
        return null;
    }
}
